﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace PRE.Catalog
{
    public partial class FrmPhieuMuon : PRE.Catalog.FrmBase
    {
       
 
        #region Variable
        DAL.EntitiesDAL.PhieuMuonDAL _dal = new DAL.EntitiesDAL.PhieuMuonDAL();
        DAL.EntitiesDAL.TheDocGiaDAL _dal2 = new DAL.EntitiesDAL.TheDocGiaDAL();
        BLL.EntitiesBLL.PhieuMuonBLL _bll = new BLL.EntitiesBLL.PhieuMuonBLL();

        DataTable _dt = new DataTable();
        DataTable _dt2 = new DataTable();
      


        #endregion

        #region Inits

        public FrmPhieuMuon()
        {
            InitializeComponent();
        }

        #region Form Load

        private void FrmBienLaiPhat_Load(object sender, EventArgs e)
        {
            PerformRefresh();
        }

        #endregion

        #endregion

        #region Override

        /// <summary>
        /// Perform when click add button
        /// </summary>
        protected override void PerformAdd()
        {
            DataTable _ma = _dal.Select_Max();
            
            if (_ma != null)
                if (_ma.Rows.Count > 0)
                {
                    string ma = _bll.AddID(_ma.Rows[0]["Ma"] + "");
                    txt_SttPhieuMuon.Text = ma;
                    ReadOnlyControl(false);
                    ClearDataBindings();
                    ChangeStatus(false);
                    cbo_MaDocGia.ResetText();
                    txt_SttPhieuMuon.Properties.ReadOnly = true;

                }

            dte_NgayMuon.EditValue = DateTime.Now;
            dte_NgayTra.EditValue = DateTime.Now.AddDays(14);
            base.PerformAdd();
            btn_Themsachmuon.Visible = true;
        }

        /// <summary>
        /// Perform when click cancel button
        /// </summary>
        protected override void PerformCancel()
        {
            ChangeStatus();
            PerformRefresh();
            base.PerformCancel();
        }

        /// <summary>
        /// Perform when click delete button
        /// </summary>
        /// <returns></returns>
        protected override void PerformDelete()
        {
            var id1 = gridView_PhieuMuon.GetFocusedRowCellValue("STT_PM") + "";

            DialogResult result;
            var ok = _dal.Delete(id1);
            result = XtraMessageBox.Show("Bạn có chắc xóa không ? ", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (result == DialogResult.OK)
            {
                if (ok)
                {
                    PerformRefresh();
                }
                else
                    XtraMessageBox.Show("Lỗi! Không xóa được", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            base.PerformDelete();
        }

        /// <summary>
        /// Perform when click edit button
        /// </summary>
        protected override void PerformEdit()
        {
            ReadOnlyControl(false);
            txt_SttPhieuMuon.Properties.ReadOnly = true;
            base.PerformEdit();
        }

        /// <summary>
        /// Perform when click print button
        /// </summary>
        protected override void PerformPrint()
        {
            try
            {
                var o = new DAL.Entities.PhieuMuon()
                  {
                      STT_PM = Convert.ToString(txt_SttPhieuMuon.Text),
                  };
                DataTable _tbPrint = new DataTable();

                string _NgayKy = "TP.Cần Thơ, ngày " + DateTime.Now.Day + " tháng " + DateTime.Now.Month + " năm " + DateTime.Now.Year;
                _tbPrint = _dal.Select_PhieuMuon(o);

                Reports.FrmReportView frm = new Reports.FrmReportView();
                frm._load_XtraPhieuMuon(_tbPrint, _NgayKy);
                frm.Show();
            }
            catch { }
            base.PerformPrint();
        }

        /// <summary>
        /// Load data or perform when click refresh button
        /// </summary>
        protected override void PerformRefresh()
        {
            ReadOnlyControl();
            LoadData();
            ChangeStatus();
            ResetText();
            btnGoSearch.Visible = false;

            if (_dt != null)
            {
                // Binding data
                ClearDataBindings();
                if (_dt.Rows.Count > 0) DataBindingControl();
            }

            base.PerformRefresh();
        }

        /// <summary>
        /// Perform when click save button
        /// </summary>
        protected override void PerformSave()
        {
            if (IsAdd)
            {
                
                var o = new DAL.Entities.PhieuMuon()
                {
                    STT_PM= Convert.ToString(txt_SttPhieuMuon.Text),
                    NGAYMUON = Convert.ToDateTime(dte_NgayMuon.Text),
                    NGAYTRA = Convert.ToDateTime(dte_NgayTra.EditValue),
                    MADOCGIA = Convert.ToString(cbo_MaDocGia.EditValue)
                };

                var oki = _dal.Insert(o);
                if (oki)
                {
                    XtraMessageBox.Show("Đã lưu thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    PerformRefresh();
                    ChangeStatus(false);
                    PerformAdd();
                }
                else
                {
                    XtraMessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    ChangeStatus(false);
                    ReadOnlyControl(false);
                }
            }

            if (IsEdit)
            {
                var id1 = gridView_PhieuMuon.GetFocusedRowCellValue("STT_PM") + "";
               

                var o = new DAL.Entities.PhieuMuon()
                {
                    STT_PM = Convert.ToString(txt_SttPhieuMuon.Text),
                    NGAYMUON = Convert.ToDateTime(dte_NgayMuon.Text),
                    NGAYTRA=Convert.ToDateTime(dte_NgayTra.EditValue),
                    MADOCGIA = Convert.ToString(cbo_MaDocGia.EditValue)

                   
                };

                var oki = _dal.Update(o);

                if (oki)
                {
                    XtraMessageBox.Show("Đã lưu thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    PerformRefresh();
                    ChangeStatus();
                }
                else
                {
                    XtraMessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    ChangeStatus(false);
                    ReadOnlyControl(false);
                }
            }


            base.PerformSave();
        }

        /// <summary>
        /// Reset all input control
        /// </summary>
        protected override void ResetText()
        {
            txt_SttPhieuMuon.ResetText();
            dte_NgayMuon.ResetText();
            dte_NgayTra.ResetText();
            cbo_MaDocGia.ResetText();
            lb_hoten.ResetText();
            lb_ngaysinh.ResetText();
            lb_gioitinh.ResetText();
            lb_diachi.ResetText();
            lb_socmnd.ResetText();
            lb_sodienthoai.ResetText();
            lb_nghenghiep.ResetText();
            lb_noicongtac.ResetText();
            base.ResetText();
        }

        /// <summary>
        /// Clear data binding
        /// </summary>
        protected override void ClearDataBindings()
        {
            txt_SttPhieuMuon.DataBindings.Clear();
            dte_NgayMuon.DataBindings.Clear();
            dte_NgayTra.DataBindings.Clear();
            cbo_MaDocGia.DataBindings.Clear();
            lb_hoten.DataBindings.Clear();
            lb_ngaysinh.DataBindings.Clear();
            lb_gioitinh.DataBindings.Clear();
            lb_diachi.DataBindings.Clear();
            lb_socmnd.DataBindings.Clear();
            lb_sodienthoai.DataBindings.Clear();
            lb_nghenghiep.DataBindings.Clear();
            lb_noicongtac.DataBindings.Clear();
            base.ClearDataBindings();
        }

        /// <summary>
        /// Add data binding
        /// </summary>
        protected override void DataBindingControl()
        {

            txt_SttPhieuMuon.DataBindings.Add("Text", _dt, ".STT_PM");
            dte_NgayMuon.DataBindings.Add("Text", _dt, ".NGAYMUON");
            dte_NgayTra.DataBindings.Add("EditValue", _dt, ".NGAYTRA");
            cbo_MaDocGia.DataBindings.Add("EditValue", _dt, ".MADOCGIA");
            lb_hoten.DataBindings.Add("Text", _dt, ".HOVATEN");
            lb_ngaysinh.DataBindings.Add("Text", _dt, ".NGAYSINH");
            lb_gioitinh.DataBindings.Add("Text", _dt, ".GIOITINH");
            lb_diachi.DataBindings.Add("Text", _dt, ".DIACHI");
            lb_socmnd.DataBindings.Add("Text", _dt, ".SCMND");
            lb_sodienthoai.DataBindings.Add("Text", _dt, ".DIENTHOAI");
            lb_nghenghiep.DataBindings.Add("Text", _dt, ".NGHENGHIEP");
            lb_noicongtac.DataBindings.Add("Text", _dt, ".NOICONGTAC");
            base.DataBindingControl();
        }

        /// <summary>
        /// Set read only control on form
        /// </summary>
        /// <param name="isReadOnly">Read only is trule else normal</param>
        protected override void ReadOnlyControl(bool isReadOnly = true)
        {
           
            txt_SttPhieuMuon.Properties.ReadOnly = isReadOnly;
           
            dte_NgayMuon.Properties.ReadOnly = isReadOnly;

            dte_NgayTra.Properties.ReadOnly = isReadOnly;
           
            cbo_MaDocGia.Properties.ReadOnly = isReadOnly;

            gridControl_PhieuMuon.Enabled = isReadOnly;

            base.ReadOnlyControl(isReadOnly);
        }

        protected override void PerformSearch()
        {
            ChangeStatus(false);
            ReadOnlyControl(false);
            ResetText();

            btnGoSearch.Visible = true;

            gridControl_PhieuMuon.Enabled = true;

            base.PerformSearch();
        }

        /// <summary>
        /// Execute Search data
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void btnGoSearch_Click(object sender, EventArgs e)
        {
           
            try
            {
                var o = new DAL.Entities.PhieuMuon()
                {
                    STT_PM = Convert.ToString(txt_SttPhieuMuon.Text),
                    //LYDOPHAT = Convert.ToString(txt_LyDo.Text),
                    //SOTIENPHAT = Convert.ToInt32(txt_SoTien.Text),
                    //NGAYMUON = Convert.ToDateTime(dte_NgayMuon.EditValue),
                    MADOCGIA = Convert.ToString(cbo_MaDocGia.EditValue)

                };

                gridControl_PhieuMuon.DataSource = _dal.Search(o);
            }
            catch (Exception)
            {

                XtraMessageBox.Show("Lỗi! Không tìm thấy phiếu mượn này", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// Update object
        /// </summary>
        /// <returns>True if successful else false</returns>
        protected override bool UpdateObject()
        {
            return base.UpdateObject();
        }

        /// <summary>
        /// Insert object
        /// </summary>
        /// <returns>True if successful else false</returns>
        protected override bool InsertObject()
        {
            return base.InsertObject();
        }

        /// <summary>
        /// Load data
        /// </summary>
        protected override void LoadData()
        {
            _dt = _dal.Select();
            _dt2 = _dal2.Select();
         
            cbo_MaDocGia.Properties.DataSource = _dt2;
           
            try
            {
                if (_dt != null)
                {
                    gridControl_PhieuMuon.DataSource = _dt;
                    gridView_PhieuMuon.OptionsBehavior.ReadOnly = true;
                    gridView_PhieuMuon.OptionsView.ColumnAutoWidth = true;
                  //  lb_hoten.Text = Convert.ToString(gridView_PhieuMuon.GetRowCellValue(0, gridView_PhieuMuon.Columns[2]));
                }

                base.LoadData();
            }
            catch (Exception ex)
            {

                XtraMessageBox.Show("Lỗi! Không load được dữ liệu", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        #endregion

        #region Thêm Sách Mượn

        private void btn_Themsachmuon_Click(object sender, EventArgs e)
        {
            foreach (var x in MdiChildren) if (x is Catalog.FrmThemSachMuon) return;
            var frm = new Catalog.FrmThemSachMuon();// { MdiParent = this };

            frm.Show();
        }

        #endregion

        
    }


}
